/**
 *====================================================
 * 文件名称: BulkstockCategoryCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年9月23日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.action.v1.dto;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockCategory;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockCategoryDetail;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockInfoDetail;
import com.yuexunit.finance.bulkstock.action.v1.model.MaterialTemplate;
import com.yuexunit.finance.bulkstock.entity.BulkstockCategoryEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockCategoryFinance;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.entity.MaterialTemplateEntity;
import com.yuexunit.finance.bulkstock.entity.lexicon.MeasurementEnum;
import com.yuexunit.finance.bulkstock.service.BulkstockCategoryService;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.bulkstock.service.MaterialTemplateService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.context.FinanceConstant;
import com.yuexunit.finance.project.entity.lexicon.FinanceStateEnum;
import com.yuexunit.finance.support.OracleConnection;
import com.yuexunit.inject.ComponentFactory;

/**
 * @ClassName: BulkstockCategoryCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月23日 下午7:32:54
 */
public class BulkstockCategoryCastor {

	public static BulkstockCategory toBulkstockCategory(
			BulkstockCategoryEntity entity, boolean material) {
		BulkstockCategory n = new BulkstockCategory();
		n.setBulkstockCategoryId(entity.getBulkstockCategoryId());
		n.setCategoryName(entity.getCategoryName());
		n.setUpdateDateTime(entity.getUpdateDateTime());
		n.setActiveState(entity.getActiveState());
		if (material) {
			MaterialTemplateService materialTemplateService = ComponentFactory
					.loadClass(MaterialTemplateService.class);
			List<MaterialTemplateEntity> list = materialTemplateService
					.inquireMaterialTemplateList(entity
							.getBulkstockCategoryId());
			for (MaterialTemplateEntity m : list) {
				n.getTemplateName().add(m.getTemplateName());
			}
		}
		return n;
	}

	public static BulkstockCategoryDetail toBulkstockCategoryDetail(
			BulkstockCategoryEntity e) {
		BulkstockCategoryDetail c = new BulkstockCategoryDetail();
		c.setCategoryName(e.getCategoryName());
		c.setUpdateDateTime(e.getUpdateDateTime());
		c.setBulkstockCategoryId(e.getBulkstockCategoryId());
		c.setIsFactoring(e.getIsFactoring());
		MaterialTemplateService materialTemplateService = ComponentFactory
				.loadClass(MaterialTemplateService.class);
		List<MaterialTemplateEntity> materialTemplateEntities = materialTemplateService
				.inquireMaterialTemplateList(c.getBulkstockCategoryId());
		for (MaterialTemplateEntity m : materialTemplateEntities) {
			MaterialTemplate t = new MaterialTemplate();
			t.setTemplateId(m.getTemplateId());
			t.setTemplateName(m.getTemplateName());
			t.setCheckedRequired(m.getCheckedRequired().ordinal());
			c.getTemplates().add(t);
		}
		return c;
	}

	public static BulkstockInfoDetail toBulkstockInfo(BulkstockInfoEntity entity) {
		BulkstockInfoDetail info = new BulkstockInfoDetail();
		BeanUtils.copyProperties(entity, info);
		info.setMeasurement(entity.getMeasurement().ordinal());
		return info;
	}

	/** 同步商品类别信息 */
	public static List<BulkstockCategoryEntity> fromSyncBulkstockCategory() {
		BulkstockCategoryService bulkstockCategoryService = ComponentFactory
				.loadClass(BulkstockCategoryService.class);
		List<BulkstockCategoryEntity> categories = new ArrayList<>();
		Connection conn = OracleConnection.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT T.FNUMBER, T.FNAME_L2, T.FID FROM T_PUB_GOODSTREE T";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String categoryFID = rs.getString("FID");
				String categoryNumber = rs.getString("FNUMBER");
				String categoryName = rs.getString("FNAME_L2");
				BulkstockCategoryEntity bulkstockCategory = bulkstockCategoryService
						.inquireFIDBulkstockCategory(categoryFID, categoryName);
				if (bulkstockCategory != null) {
					continue;
				}
				bulkstockCategory = new BulkstockCategoryEntity();
				bulkstockCategory.setCategoryFID(categoryFID);
				bulkstockCategory.setCategoryName(categoryName);
				bulkstockCategory.setCategoryNumber(categoryNumber);
				categories.add(bulkstockCategory);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
			}
			try {
				if (pstmt != null) {
					pstmt.close();
				}
			} catch (Exception e) {
			}
			OracleConnection.getInstance().closeConnetion(conn);
		}
		return categories;
	}

	/** 同步商品信息 */
	public static List<BulkstockInfoEntity> fromSyncBulkstockInfo(
			BulkstockCategoryEntity category) {
		BulkstockInfoService bulkstockService = ComponentFactory
				.loadClass(BulkstockInfoService.class);
		List<BulkstockInfoEntity> bulkstocks = new ArrayList<>();
		Connection conn = OracleConnection.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT T.FNUMBER, T.FNAME_L2, T.CFHEPLCODE FROM CT_PUB_GOODS T WHERE T.FTREEID = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, category.getCategoryFID());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String bulkstockHelpCode = rs.getString("CFHEPLCODE");
				String bulkstockNumber = rs.getString("FNUMBER");
				String bulkstockName = rs.getString("FNAME_L2");
				List<BulkstockInfoEntity> bulkstockInfos = bulkstockService
						.inquireBulkstockInfo(
								category.getBulkstockCategoryId(),
								bulkstockName);
				if (bulkstockInfos != null && !bulkstockInfos.isEmpty()) {
					continue;
				}
				BulkstockInfoEntity bulkstock = new BulkstockInfoEntity();
				bulkstock.setBulkstockCategoryId(category
						.getBulkstockCategoryId());
				bulkstock.setCategoryName(category.getCategoryName());
				bulkstock.setBulkstockHelpCode(bulkstockHelpCode);
				bulkstock.setBulkstockName(bulkstockName);
				bulkstock.setBulkstockNumber(bulkstockNumber);
				bulkstock.setMeasurement(MeasurementEnum.WEIGHT); // 默认重量计价
				bulkstock
						.setConversionFactor(FinanceConstant.BULKSTOCK_CONVERSIONFACTOR);
				bulkstocks.add(bulkstock);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
			}
			try {
				if (pstmt != null) {
					pstmt.close();
				}
			} catch (Exception e) {
			}
			OracleConnection.getInstance().closeConnetion(conn);
		}
		return bulkstocks;
	}

	public static void existUnfinishFinance(Long bulkstockInfoId) {
		BulkstockCategoryService bulkstockCategoryService = ComponentFactory
				.loadClass(BulkstockCategoryService.class);
		List<BulkstockCategoryFinance> bulkstockCategoryFinances = bulkstockCategoryService
				.existUnfinishFinance(bulkstockInfoId);
		for (BulkstockCategoryFinance bulkstockCategoryFinance : bulkstockCategoryFinances) {
			if (bulkstockCategoryFinance.getFinanceState().compareTo(
					FinanceStateEnum.FINISH)==0
					|| bulkstockCategoryFinance.getFinanceState().compareTo(
							FinanceStateEnum.EVALUATE)==0
					|| bulkstockCategoryFinance.getFinanceState().compareTo(
							FinanceStateEnum.CANCEL)==0) {
					throw new BusinessException("此大宗商品在运行中的融资申请存在,不允许停用");
			}
		}
	}

}
